<%@page contentType="text/html;charset=utf-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <jsp:include page="common.jsp"/>
    <style type="text/css">
        .cque-list {
            margin: 10px;
            display: flex;
            flex-direction: column;
        }

        .cque-list-nav {
            margin: 10px;
        }

        .cque-list-content {
            margin: 10px;
        }

        .cque-td {
            display: flex;
            justify-content: space-around;
            align-items: center;
        }

        th {
            text-align: center;
        }

        tr {
            text-align: center;
        }
    </style>
</head>

<body>
<div class="cque-window">
    <div class="cque-nav">
        <div class="navbar-header" style="margin-left: 20px">
            <img alt="Brand" height="60px" src="${pageContext.request.contextPath}/assets/images/logo.png">
        </div>
        <h2 style="margin-left: 20px">商城后台管理系统</h2>
    </div>
    <div class="cque-content">
        <div class="cque-side">
            <div class="panel panel-info" style="margin: 0;height: 100%">
                <a href="/admin/category/list"><div class="panel-heading" style="background-color:#d9edf7;">系统菜单</div></a>

                <div class="panel-body" style="padding: 0">
                    <div class="list-group">
                        <a href="/admin/category/list" class="list-group-item">商品类别管理</a>
                        <a href="/admin/product/list" class="list-group-item" style="background-color:#d9edf7;" >商品管理</a>
                        <a href="/admin/order/list" class="list-group-item">订单管理</a>
                    </div>
                </div>
            </div>
        </div>
        <div class="cque-main">
            <div class="cque-list">
                <div class="cque-list-nav">
                    <button type="button" class="btn btn-default " data-toggle="modal"
                            data-target="#myModa2">添加
                    </button>
                </div>
                <div class="cque-list-content">
                    <table class="table table-bordered" cellpadding="0">
                        <thead>
                        <tr>
                            <th>编号</th>
                            <th>商品名称</th>
                            <th >市场价</th>
                            <th >商城价</th>
                            <th >商品图片地址</th>
                            <th >上架时间</th>
                            <th >是否热销</th>
                            <th >上商品描述</th>
                            <th >是否上架</th>
                            <th >商品分类</th>
                            <th class="col-sm-2">操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <c:forEach items="${productList.list}" var="list">
                            <tr class="p">
                                <td>${list.id}</td>
                                <td >${list.name}</td>
                                <td >${list.marketPrice}</td>
                                <td >${list.shopPrice}</td>
                                <td >${list.image}</td>
                                <td >${list.createDate}</td>
                                <td >${list.isHot}</td>
                                <td >${list.desc}</td>
                                <td >${list.flag}</td>
                                <td >${list.cid}</td>
                                <td >
                                    <button type="button" class="update btn btn-default " data-toggle="modal"
                                            data-target="#myModal"> 修改
                                    </button>
                                    <button class="btn btn-default" onclick="del(${list.id})" type="button">删除</button>

                                </td>
                            </tr>
                        </c:forEach>


                        </tbody>
                    </table>

                    <nav aria-label="" class="text-center">
                        <ul class="pagination">
                            <c:if test="${productList.pageNum==1}">
                                <li class="disabled"><span><a aria-hidden="true">&laquo;</a> </span></li>
                            </c:if>
                            <c:if test="${productList.pageNum!=1}">
                                <li><span><a href="/admin/product/list?pageNum=${productList.pageNum-1}&pageSize=4" aria-hidden="true">&laquo;</a> </span></li>
                            </c:if>


                            <c:forEach begin="1" end="${productList.pages}" step="1" varStatus="status">
                                <c:if test="${productList.pageNum==status.count}">
                                    <li class="active">
                                        <a>${status.count}</a>
                                    </li>
                                </c:if>
                                <c:if test="${productList.pageNum!=status.count}">
                                    <li>
                                        <a href="/admin/product/list?pageNum=${status.count}&pageSize=4">${status.count}</a>
                                    </li>
                                </c:if>

                            </c:forEach>


                            <c:if test="${productList.pageNum==productList.pages}">
                                <li class="disabled"><span> <a aria-hidden="true">&raquo;</a> </span></li>
                            </c:if>
                            <c:if test="${productList.pageNum!=productList.pages}">
                                <li><span> <a href="/admin/product/list?pageNum=${productList.pageNum+1}&pageSize=4" aria-hidden="true">&raquo;</a> </span></li>
                            </c:if>
                         <li class="active">  <span class=" page_num_inf">
                                共 <span>${productList.pageNum}</span> 页
                        </span></li>
                            <li class="active"> <span class=" page_num_inf">共 <span>${productList.total}</span>条</span></li>
                        </ul>

                    </nav>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 修改Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">修改商品类别</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal ">
                    <div class="form-group">
                        <label for="pid" class="col-sm-offset-1 col-sm-3 control-label">商品ID</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="pid"
                                   readonly="readonly">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="pname" class="col-sm-offset-1 col-sm-3 control-label">商品名称</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="pname">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="market_price" class="col-sm-offset-1 col-sm-3 control-label">商品市场价格</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="market_price">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="shop_price" class="col-sm-offset-1 col-sm-3 control-label">商品商城价格</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="shop_price">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="pimage" class="col-sm-offset-1 col-sm-3 control-label">商品图片地址</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="pimage">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="pdate" class="col-sm-offset-1 col-sm-3 control-label">商品上线日期</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="pdate" readonly="readonly">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="is_hot" class="col-sm-offset-1 col-sm-3 control-label">商品是否热销</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="is_hot">
                        </div>
                    </div>


                    <div class="form-group">
                        <label for="pdesc" class="col-sm-offset-1 col-sm-3 control-label">商品描述</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="pdesc">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="pflag" class="col-sm-offset-1 col-sm-3 control-label">商品是否上架</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="pflag">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="cid" class="col-sm-offset-1 col-sm-3 control-label">商品类别编号</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="cid">
                        </div>
                    </div>

                </form>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" onclick="save()">保存</button>
                </div>
            </div>
        </div>
    </div>
</div>
<%--添加--%>
<div class="modal fade" id="myModa2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabe2">新增商品</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal ">
                    <div class="form-group">
                        <label for="pid2" class="col-sm-offset-1 col-sm-3 control-label">商品ID</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="pid2" value="" placeholder="请输入商品ID"
                            >
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="pname2" class="col-sm-offset-1 col-sm-3 control-label">商品名称</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="pname2" value="" placeholder="请输入商品名称">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="market_price2" class="col-sm-offset-1 col-sm-3 control-label">商品市场价格</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="market_price2" value="" placeholder="请输入商品市场价格">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="shop_price2" class="col-sm-offset-1 col-sm-3 control-label">商品商城价格</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="shop_price2" value="" placeholder="请输入商品商城价格">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="pimage2" class="col-sm-offset-1 col-sm-3 control-label">商品图片地址</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="pimage2" value="" placeholder="请输入商品图片地址">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="is_hot2" class="col-sm-offset-1 col-sm-3 control-label">商品是否热销</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="is_hot2" value="" placeholder="1（热销）,0（不热销）">
                        </div>
                    </div>


                    <div class="form-group">
                        <label for="pdesc2" class="col-sm-offset-1 col-sm-3 control-label">商品描述</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="pdesc2" value="" placeholder="请输入商品描述">
                        </div>
                    </div>
                    <%--                    <div class="form-group">--%>
                    <%--                        <label for="pdate2" class="col-sm-offset-1 col-sm-3 control-label">商品上架时间</label>--%>
                    <%--                        <div class="col-sm-6">--%>
                    <%--                            <input type="text" class="form-control" id="pdate2" value="">--%>
                    <%--                        </div>--%>
                    <%--                    </div>--%>

                    <div class="form-group">
                        <label for="pflag2" class="col-sm-offset-1 col-sm-3 control-label">商品是否上架</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="pflag2" value="" placeholder="0（上架）,1（下架）">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="cid2" class="col-sm-offset-1 col-sm-3 control-label">商品类别编号</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="cid2" value="" placeholder="请输入商品类别编号">
                        </div>
                    </div>

                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="add btn  btn-primary">保存</button>
            </div>
        </div>
    </div>
</div>

<script>
    $('.add').click(function () {
        var pid = $('#pid2').val();
        var pname = $('#pname2').val();
        var market_price = $('#market_price2').val();
        var shop_price = $('#shop_price2').val();
        var pimage = $('#pimage2').val();
        var isHot = $('#is_hot2').val();
        var pdesc = $('#pdesc2').val();
        var pflag = $('#pflag2').val();
        var cid = $('#cid2').val();

        // console.log(pid+pname+market_price+shop_price+pimage+pdate+isHot+pdesc+pflag+cid)
        $.post('/admin/product/add', {
            pid: pid, pname: pname, market_price: market_price
            , shop_price: shop_price, pimage: pimage, isHot: isHot,
            pdesc: pdesc, pflag: pflag, cid: cid
        }, function (result) {
            if (result.code === 1) {
                location.href = '/admin/product/list'
            } else {
                alert('修改失败，请重试...')
                location.href = '/admin/product/list'
            }
        })
    })


    $('.update').click(function () {
        var pid = $(this).parent().parent().children().eq(0).text();
        var pname = $(this).parent().parent().children().eq(1).text();
        var market_price = $(this).parent().parent().children().eq(2).text();
        var shop_price = $(this).parent().parent().children().eq(3).text();
        var pimage = $(this).parent().parent().children().eq(4).text();
        var pdate = $(this).parent().parent().children().eq(5).text();
        var isHot = $(this).parent().parent().children().eq(6).text();
        var pdesc = $(this).parent().parent().children().eq(7).text();
        var pflag = $(this).parent().parent().children().eq(8).text();
        var cid = $(this).parent().parent().children().eq(9).text();
        $('#pid').val(pid);
        $('#pname').val(pname);
        $('#market_price').val(market_price);
        $('#shop_price').val(shop_price);
        $('#pimage').val(pimage);
        $('#pdate').val(pdate);
        $('#is_hot').val(isHot);
        $('#pdesc').val(pdesc);
        $('#pflag').val(pflag);
        $('#cid').val(cid);
    })

    function del(pid) {
        console.log(pid)
        if (confirm('是否确认删除?')) {
            $.post('/admin/product/del', {pid: pid}, function (result) {
                if (result.code === 1) {
                    location.href = '/admin/product/list'
                }
            })
        } else {
            return false;
            location.href = '/admin/product/list'
        }
    }

    function save() {
        var pid = $('#pid').val();
        var pname = $('#pname').val();
        var market_price = $('#market_price').val();
        var shop_price = $('#shop_price').val();
        var pimage = $('#pimage').val();
        var pdate = $('#pdate').val();
        var isHot = $('#is_hot').val();
        var pdesc = $('#pdesc').val();
        var pflag = $('#pflag').val();
        var cid = $('#cid').val();
        $.post('/admin/product/updates', {
            pid: pid, pname: pname, market_price: market_price
            , shop_price: shop_price, pimage: pimage, pdate: pdate, isHot: isHot,
            pdesc: pdesc, pflag: pflag, cid: cid
        }, function (result) {
            if (result.code === 1) {
                location.href = '/admin/product/list'
            } else {
                alert('修改失败，请重试...')
                location.href = '/admin/product/list'
            }
        })

    }
</script>
</body>
</html>